package com.cskaoyan.controller.admin;

import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.model.admin.bo.AdminCreateBo;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.AdminCreateVo;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.model.admin.vo.SystemAdminListVo;
import com.cskaoyan.service.admin.SystemAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 18:50
 */
@RestController
@RequestMapping("admin/admin")
public class SystemAdminController {
    @Autowired
    SystemAdminService systemAdminService;

    @Autowired
    AdminStorageController adminStorageController;


    @RequestMapping("/list")
    public BaseRespVo getAdminList(String username, BaseParam baseParam) {
        SystemAdminListVo list = systemAdminService.getList(username, baseParam);
        return BaseRespVo.ok(list);
    }

    @RequestMapping("/create")
    public BaseRespVo createAdmin(@RequestBody AdminCreateBo adminCreateBo) {
        AdminCreateVo adminCreateVo = systemAdminService.createAdmin(adminCreateBo);
        return BaseRespVo.ok(adminCreateVo);
    }

    @RequestMapping("/update")
    public BaseRespVo updateAdmin(@RequestBody AdminCreateBo adminCreateBo) {
        if (adminCreateBo.getUsername().length() < 6) {
            return BaseRespVo.fail(601, "管理员名称不符合规定");
        }
        if (adminCreateBo.getPassword().length() < 6) {
            return BaseRespVo.fail(602, "管理员密码长度不能小于6");
        }
        AdminCreateVo admincreateVo = systemAdminService.updateAdmin(adminCreateBo);
        return BaseRespVo.ok(admincreateVo);
    }

    @RequestMapping("/delete")
    public BaseRespVo deleteAdmin(@RequestBody AdminCreateBo adminCreateBo, HttpSession session){
        systemAdminService.deleteAdmin(adminCreateBo,session);
        adminStorageController.deleteStorage(adminCreateBo);
        return BaseRespVo.ok(null);
    }
}
